package com.guanyong.webapi.controllers.api;

import com.guanyong.model.*;
import com.guanyong.services.*;
import com.guanyong.utils.Comm.ResultCode;
import com.guanyong.utils.Comm.ResultObject;
import com.guanyong.webapi.config.CreateVideoURLUtil;
import com.guanyong.webapi.config.VideoLocalUtil;
import com.guanyong.webapi.config.VoucherUtil;
import com.guanyong.webapi.handle.UserHandle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mxk on 2017/6/27.
 */
@RestController
@RequestMapping("/api/video/")
public class VideoApi {

    @Autowired
    VideoSourceService videoSourceService;


    @Autowired
    VideoTopLogService videoTopLogService;

    @Autowired
    VideoTodayService videoTodayService;

    @Autowired
    CreateVideoURLUtil createVideoURLUtil;

    //推荐视频  iOS使用
    @RequestMapping(value = "getvideotop", method = RequestMethod.GET)
    public ResultObject<HashMap<String,Object>> GetVideoTop(
            @RequestParam Integer classtype,
            @RequestParam int pno,
            @RequestParam int pagesize,
            @RequestParam int allcount){
        ResultObject<HashMap<String,Object>> resultObject=new ResultObject<HashMap<String,
                Object>>();
        HashMap<String ,Object> hashMap=
                videoTopLogService.getVideoTopLogList(classtype,pno,pagesize,allcount);
        resultObject.setResult(hashMap);
        return  resultObject;

    }


    @RequestMapping(value = "getvideotop1_0", method = RequestMethod.GET)
    public ResultObject<HashMap<String,Object>> GetVideoTop1_0(
            @RequestParam Integer classtype,
            @RequestParam int pno,
            @RequestParam int pagesize,
            @RequestParam int allcount){
        ResultObject<HashMap<String,Object>> resultObject=new ResultObject<HashMap<String,
                Object>>();
        HashMap<String ,Object> hashMap=
                videoTopLogService.getVideoTopLogList(classtype,pno,pagesize,allcount);
        try {
            List<VideoTopLogModel> videoTopLogModellList  =(List<VideoTopLogModel>) hashMap.get("list");
            for (VideoTopLogModel videoTopLogModel :videoTopLogModellList){
                VideoSourceModel videoSourceModel=videoSourceService.
                        GetVideoScorceByObjectIdType(classtype,
                                videoTopLogModel.getfObjectid(),2);
                String playAuth = null;
                if (videoSourceModel.getfSourceurl()!=null){
                    playAuth=createVideoURLUtil.getVideoURL(videoSourceModel.getfSourceLocal());
                }
                FilmModel filmModel = (FilmModel) videoTopLogModel.getfObject();
//                filmModel.setfSourceurl(videoSourceModel.getfSourceLocal());
                filmModel.setfSourceurl(playAuth);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        resultObject.setResult(hashMap);
        return  resultObject;

    }

    @RequestMapping(value = "getvideoToday", method = RequestMethod.GET)
    public ResultObject<HashMap<String,Object>> GetVideoToday(
            @RequestParam Integer classtype,
            @RequestParam int pno,
            @RequestParam int pagesize,
            @RequestParam int allcount){
        ResultObject<HashMap<String,Object>> resultObject=new ResultObject<HashMap<String,
                Object>>();
        HashMap<String ,Object> hashMap=
                videoTodayService.getVideoTodayList(classtype,pno,pagesize,allcount);

        resultObject.setResult(hashMap);
        return  resultObject;

    }


    @RequestMapping(value = "getvideosource", method = RequestMethod.GET)
    public ResultObject<List<VideoSourceModel>> GetVideoSourceList
            (@RequestParam String objectid,
             @RequestParam Integer classtype){
        ResultObject<List<VideoSourceModel>> resultObject=new
                ResultObject<List<VideoSourceModel>>();
        List<VideoSourceModel> sourceModelList=videoSourceService.
                GetVideoScorceByObjectId(classtype,
                        objectid,0);
        resultObject.setResult(sourceModelList);
        return  resultObject;

    }
    @RequestMapping(value = "getvideosourceclips", method = RequestMethod.POST)
    public ResultObject<Map<String,Object>> GetVideoSourceClips
            (@RequestParam String objectid,
             @RequestParam Integer classtype){
        ResultObject<Map<String,Object>> resultObject=new
                ResultObject<Map<String,Object>>();
        VideoSourceModel videoSourceModel=videoSourceService.
                GetVideoScorceByObjectIdType(classtype,
                        objectid,2);
        if (videoSourceModel!=null){
            Map<String,Object> map = new HashMap<String, Object>();
            String playAuth = "";
            if (videoSourceModel.getfSourceurl()!=null){
                playAuth=createVideoURLUtil.getVideoURL(videoSourceModel.getfSourceLocal());
            }
            if (playAuth!=null){
                map.put("objectId",objectid);
                map.put("videoId",playAuth);
                map.put("videoVoucher",playAuth);
                resultObject.setResult(map);
            }else {
                resultObject.setCode(ResultCode.NoResult);
            }
        }else{
            resultObject.setCode(ResultCode.NoResult);
        }
        return  resultObject;

    }


    @RequestMapping(value = "getvideoclips", method = RequestMethod.GET)
    public ResultObject<HashMap<String,Object>> getVideoClips(
            @RequestParam int pno,
            @RequestParam int pagesize,
            @RequestParam int allcount){
        ResultObject<HashMap<String,Object>> resultObject=new ResultObject<HashMap<String,
                Object>>();
        HashMap<String ,Object> hashMap=
                videoSourceService.getVideoClipsList(pno,pagesize,allcount);
        resultObject.setResult(hashMap);
        return  resultObject;

    }



    @RequestMapping(value = "getvideoclipsList", method = RequestMethod.GET)
    public ResultObject<HashMap<String,Object>> GetVideoClips(
            @RequestParam int pno,
            @RequestParam int pagesize,
            @RequestParam int allcount){
        ResultObject<HashMap<String,Object>> resultObject=new ResultObject<HashMap<String,
                Object>>();
        HashMap<String ,Object> hashMap=
                videoSourceService.getVideoClipsList(pno,pagesize,allcount);
        try {
            List<FilmModel> filmModelList  =(List<FilmModel>) hashMap.get("list");
            for (FilmModel filmModel :filmModelList){
                String playAuth = null;
                if (filmModel.getfSourceurl()!=null){
                    playAuth=createVideoURLUtil.getVideoURL(filmModel.getfSourceurl());
                }
//                filmModel.setfSourceurl(videoSourceModel.getfSourceLocal());
                filmModel.setfSourceurl(playAuth);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        resultObject.setResult(hashMap);
        return  resultObject;

    }

}
